package org.sgx.dao.test.test1;

import java.util.Date;
import java.util.Locale;

import org.sgx.dao.BeanDao;
import org.sgx.dao.ConnectionGiver;
import org.sgx.dao.Dao;
import org.sgx.dao.DaoException;
import org.sgx.dao.Relationship;
import org.sgx.dao.model.Model;
import org.sgx.dao.test.test1.testModel.Leaf;
import org.sgx.dao.test.test1.testModel.Tree;
import org.sgx.dao.util.SQLUtil;
import org.sgx.dao.util.Util;

import junit.framework.TestCase;

public class RelationTest1  extends TestCase {
	
public void testNoLocales() throws DaoException {
ConnectionGiver cg = DaoTest1.getValidConnectionGiver();
SQLUtil sql = new SQLUtil(cg);

String leafTable = "RelationTest1_testNoLocales_leaf",
treeTable = "RelationTest1_testNoLocales_tree", 
treeLeaves_rel_tableName = "RelationTest1_testNoLocales_treeLeaves_rel";



//create some model
Leaf leaf1 = new Leaf("green1", new Date()), 
	leaf2 = new Leaf("brown", new Date());
Tree tree1 = new Tree("eucaliptus"), 
	tree2 = new Tree("pine-tree");

//create the dao for this models
Dao<Leaf> leafDao = new BeanDao<Leaf>(cg, Leaf.class, leafTable);
Dao<Tree> treeDao = new BeanDao<Tree>(cg, Tree.class, treeTable);

try {
	//first we drop the table and then recreate the table in database for having a clean table to work with
	leafDao.dropTable(); 
	leafDao.createTable();
	treeDao.dropTable(); 
	treeDao.createTable();
	sql.tableDrop(treeLeaves_rel_tableName);
} catch (Exception e) {
	// TODO: handle exception
}


//add all trees and leaves to database
leafDao.add(leaf1, leaf2);
treeDao.add(tree1, tree2);

//now declare a relationship between Trees (relationship's source) and Leafs (relationship's end).
Relationship<Tree, Leaf> treeLeaves = new Relationship<Tree, Leaf>(
	"leaves", treeDao, leafDao, treeLeaves_rel_tableName);

assertTrue(treeLeaves.getEnds(tree1).size()==0);
treeLeaves.add(tree1, leaf1);
assertTrue(treeLeaves.getEnds(tree1).size()==1);
assertTrue(treeLeaves.getEnds(tree1).get(0).getColor().equals("green1"));

//update the leaf and check the relationship again
leaf1.setColor("red");
leafDao.update(leaf1);
assertTrue(treeLeaves.getEnds(tree1).size()==1);
assertTrue(treeLeaves.getEnds(tree1).get(0).getColor().equals("red"));

treeLeaves.add(tree1, leaf2);
assertTrue(treeLeaves.getEnds(tree1).size()==2);

//remove
treeLeaves.remove(tree1, leaf2);
assertTrue(treeLeaves.getEnds(tree1).size()==1);

treeLeaves.removeSource(tree1);
assertTrue(treeLeaves.getEnds(tree1).size()==0);

treeLeaves.add(tree1, leaf1);
treeLeaves.add(tree1, leaf2);
treeLeaves.add(tree2, leaf2);
assertEquals(2, treeLeaves.getEnds(tree1).size());
assertEquals(1, treeLeaves.getEnds(tree2).size());
assertEquals(2, treeLeaves.getSources(leaf2).size());
treeLeaves.removeEnd(leaf2);
assertEquals(1, treeLeaves.getEnds(tree1).size());
assertEquals(0, treeLeaves.getEnds(tree2).size());
assertEquals(0, treeLeaves.getSources(leaf2).size());

//use the the relationship in the other direction. relate leaf1 with another tree tree2
assertTrue(treeLeaves.getEnds(tree2).size()==0);
assertTrue(!treeLeaves.related(tree2, leaf1));
treeLeaves.add(tree2, leaf1);
assertTrue(treeLeaves.related(tree2, leaf1));
assertTrue(treeLeaves.getEnds(tree2).size()==1 && 
	treeLeaves.getEnds(tree2).get(0).getColor().equals("red"));
	
}

public void testLocales() throws DaoException {
	ConnectionGiver cg = DaoTest1.getValidConnectionGiver();
	SQLUtil sql = new SQLUtil(cg);
	
	String treeTableName = "RelationTest1_testLocales_Tree", 
		leafTableName = "RelationTest1_testLocales_Leaf", 
		relTableName = "RelationTest1_testLocales_TreeLeafRel",
		localeUs = Locale.US.toString(), 
		localeEs = "es_ES";	
		
	//create the dao for this models
	Dao<Leaf> leafDao = new BeanDao<Leaf>(cg, Leaf.class, treeTableName);
	Dao<Tree> treeDao = new BeanDao<Tree>(cg, Tree.class, leafTableName);
	
	try {
		//first we drop the table and then recreate the table in database for having a clean table to work with
		leafDao.dropTable(); 
		leafDao.createTable();
		treeDao.dropTable(); 
		treeDao.createTable();
		sql.tableDrop(relTableName);
	} catch (Exception e) {
		// TODO: handle exception
	}
	
	//create some model
	Leaf leaf1 = new Leaf("green", new Date()), 
		leaf2 = new Leaf("brown", new Date());
	Tree tree1 = new Tree("poplar"), 
		tree2 = new Tree("pine-tree");
	

	
	/* we add the objects using setLocale */
	Long leaf1Id = leafDao.setLocale(leaf1, localeUs);
	Long leaf2Id = leafDao.setLocale(leaf2, localeUs);
	/* update localized properties and create a new locale */
	leaf1.setColor("verde"); 
	leaf2.setColor("marrón");	
	leafDao.setLocale(leaf1, localeEs);
	leafDao.setLocale(leaf2, localeEs);
	
	Leaf leaf1_us = leafDao.getLocale(leaf1Id, localeUs), 
		leaf1_es = leafDao.getLocale(leaf1Id, localeEs);
	//simple locale test: two objects with same id, but different locale
	assertEquals(leaf1_us.getLeafId(), leaf1_es.getLeafId());
	assertEquals( "green", leaf1_us.getColor());
	assertEquals( "verde", leaf1_es.getColor());
	
	Leaf leaf3 = new Leaf("yellow1", new Date());
	Long leaf3Id = leafDao.add(leaf3);
	assertEquals(2, leafDao.getAllLocale(localeEs).size());//there are only two localized leafs, because the yellow one was not localized.
	leafDao.setLocale(leaf3, localeUs);
	assertEquals(2, leafDao.getAllLocale(localeEs).size());
	leafDao.setLocale(leaf3, localeEs);
	assertEquals(3, leafDao.getAllLocale(localeEs).size());
	
	treeDao.setLocale(tree1, localeUs);
	treeDao.setLocale(tree2, localeUs);
	tree1.setSpecie("álamo");
	tree2.setSpecie("pino");
	treeDao.setLocale(tree1, localeEs);
	treeDao.setLocale(tree2, localeEs);
		
	//searchLocale tests
	assertEquals(0, treeDao.searchLocale(Util.toOrderedMap("specie", "pino"), localeUs).size());	
	assertEquals(1, treeDao.searchLocale(Util.toOrderedMap("specie", "pino"), localeEs).size());
	assertEquals("pino", treeDao.searchLocale(Util.toOrderedMap("specie", "pino"), localeEs).get(0).getSpecie());
	
	Relationship<Tree, Leaf> treeLeaves = new Relationship<Tree, Leaf>(
		"leaves", treeDao, leafDao, relTableName);		
	
	/** associate some trees with leaf, without taking care about localization */
	treeLeaves.add(tree1, leaf1);	
	assertEquals(1, treeLeaves.getEnds(tree1).size());
	assertEquals(1, treeLeaves.getSources(leaf1).size());
	//now get localized associated objects.
	assertEquals(1, treeLeaves.getLocalizedEnds(tree1, localeEs).size());
	assertEquals(1, treeLeaves.getLocalizedEnds(tree1, localeUs).size());
	assertEquals(1, treeLeaves.getLocalizedSources(leaf1, localeEs).size());
	assertEquals(1, treeLeaves.getLocalizedSources(leaf1, localeUs).size());
	assertEquals("green", treeLeaves.getLocalizedEnds(tree1, localeUs).get(0).getColor());
	assertEquals("verde", treeLeaves.getLocalizedEnds(tree1, localeEs).get(0).getColor());
	assertEquals("poplar", treeLeaves.getLocalizedSources(leaf1, localeUs).get(0).getSpecie());
	assertEquals("álamo", treeLeaves.getLocalizedSources(leaf1, localeEs).get(0).getSpecie());

}


}
